package com.galaxy.commom.base.exception;

import com.galaxy.commom.base.message.ResponseCode;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 请求异常类
 * @author caijinbang
 * @date 2018/10/27 17:10
 */
@Data
@Builder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class RequestException extends RuntimeException implements Serializable {

  private Integer status;
  private String msg;
  private Exception e;

  public RequestException(ResponseCode statusEnum, Exception e) {
    this.status = statusEnum.code;
    this.msg = statusEnum.msg;
    this.e = e;
  }


  public RequestException(ResponseCode statusEnum) {
    this.status = statusEnum.code;
    this.msg = statusEnum.msg;
  }

  public synchronized static RequestException fail(String msg){
    return RequestException.builder()
        .status(ResponseCode.FAIL.code)
        .msg(msg)
        .build();
  }

  public synchronized static RequestException fail(String msg,Exception e){
    return RequestException.builder()
        .status(ResponseCode.FAIL.code)
        .msg(msg)
        .e(e)
        .build();
  }

  public synchronized static RequestException fail(Integer code,String msg,Exception e){
    return RequestException.builder()
        .status(code)
        .msg(msg)
        .e(e)
        .build();
  }
}
